package com.benyanyi.permissionlib;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

import com.benyanyi.permissionlib.callback.PermissionCallBack;
import com.benyanyi.permissionlib.dialog.PermissionDefaultDialogInfo;
import com.benyanyi.permissionlib.dialog.PermissionDialogInfo;

/**
 * @author YanYi
 * @date 2018-06-06 10:40
 * @email ben@yanyi.red
 * @overview 权限获取帮助类
 */
public class PermissionHelper implements PermissionConfig {
    private static PermissionConfig instance;
    private static Activity mActivity;
    private static final String tag = "PermissionHelper";
    private final PermissionFragment permissionFragment;

    private PermissionHelper() {
        permissionFragment = getFragment();
    }

    public static PermissionConfig with(Activity activity) {
        if (instance == null) {
            mActivity = activity;
            instance = new PermissionHelper();
        }
        if (mActivity == null) {
            mActivity = activity;
            instance = new PermissionHelper();
        }
        if (activity != mActivity) {
            mActivity = activity;
            instance = new PermissionHelper();
        }
        return instance;
    }

    public static PermissionConfig with(Fragment fragment) {
        return with(fragment.getActivity());
    }

    public static PermissionConfig with(androidx.fragment.app.Fragment fragment){
        return with(fragment.getActivity());
    }

    private PermissionFragment getFragment() {
        PermissionFragment fragment = (PermissionFragment) mActivity.getFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            fragment = new PermissionFragment();
            FragmentManager fragmentManager = mActivity.getFragmentManager();
            fragmentManager
                    .beginTransaction()
                    .add(fragment, tag)
                    .commitAllowingStateLoss();
            fragmentManager.executePendingTransactions();
        }
        return fragment;
    }

    @Override
    public PermissionConfig setPermissions(String... permissions) {
        this.permissionFragment.setPermissions(permissions);
        return this;
    }

    @Override
    public PermissionConfig setPermissionDialogInfo(PermissionDialogInfo info) {
        this.permissionFragment.setInfo(info);
        return this;
    }

    /**
     * 使用默认dialog
     *
     * @return
     */
    @Override
    public PermissionConfig setDefaultPermissionDialogInfo() {
        this.permissionFragment.setInfo(PermissionDefaultDialogInfo.getInfo(mActivity));
        return this;
    }

    @Override
    public void request(PermissionCallBack callBack) {
        this.permissionFragment.setCallBack(callBack);
        this.permissionFragment.startForPermissionResult();
    }

    /**
     * 解绑 Activity
     */
    static void removeFragment() {
        if (instance != null && mActivity != null) {
            Fragment fragment = mActivity.getFragmentManager().findFragmentByTag(tag);
            if (fragment != null) {
                mActivity.getFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
            }
        }
    }

    public static void destroy() {
        removeFragment();
        mActivity = null;
        instance = null;
    }
}
